package com.dt76.smallloanbackenduser.controller.etao;

import com.dt76.smallloanbackenduser.pojo.etao.CurrentUser;
import com.dt76.smallloanbackenduser.pojo.etao.Department;
import com.dt76.smallloanbackenduser.pojo.etao.Employee;
import com.dt76.smallloanbackenduser.remote.etao.EmployeeRemote;
import com.dt76.smallloanbackenduser.remote.etao.PrivilegeManagement;
import com.dt76.smallloanbackenduser.utils.Page;
import com.dt76.smallloanbackenduser.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmployeeController {

    //注入员工远程调用服务
    @Autowired
    private EmployeeRemote employeeRemote;
    //注入部门远程调用服务
    @Autowired
    private PrivilegeManagement privilegeManagement;

    @RequestMapping("/getEmpById")
    @ResponseBody
    public Employee getEmpById(Integer eId) {
        return employeeRemote.getEmpById(eId);
    }
    @RequestMapping("/getAllEmp")
    public String getAllEmp(Model model,
                            @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                            @RequestParam(defaultValue = "", value = "empName") String empName,
                            @RequestParam(defaultValue = "0", value = "dId") Integer dId) {
        //判断条件
        String selEmpName;
        if (!empName.equals("0") && empName != null) {
            selEmpName = empName;
            empName = "%" + empName + "%";
        } else {
            empName = "";
            selEmpName = empName;
        }
        List<Department> deptPage = privilegeManagement.getDept();
        Page page = employeeRemote.getPageEmp(empName, dId, pageNum);
        model.addAttribute("deptPage", deptPage);
        model.addAttribute("empPage", page.getRows());
        model.addAttribute("pageInfo", page);
        //返回查询条件
        model.addAttribute("selDId", dId);
        model.addAttribute("selEmpName", selEmpName);
        List<Integer> pageArr = new ArrayList<Integer>();
        for (Integer i = 0; i < page.getTotalPageCount(); i++) {
            pageArr.add(i + 1);
        }
        model.addAttribute("pageArr", pageArr);
        return "pages/etao/employee";
    }

    //带条件查询
    @RequestMapping("/selectPageBy")
    @ResponseBody
    public String selectPageBy(Integer pageNum, String empName, Integer dId) {
        return "/emp/getAllEmp?pageNum=" + pageNum + "&empName=" + empName + "&dId=" + dId;
    }

    //修改员工updEmpEnable状态
    @RequestMapping("/updEmpEnable")
    @ResponseBody
    public Result updEmpEnable(Integer empId, Integer updEnable) {
        Result result = employeeRemote.updEmpEnable(empId, updEnable);
        if (result.isSuccess()) {
            result.setMsg("修改成功");
        } else {
            result.setMsg("修改失败");
        }
        return result;
    }

    //修改和新增员工
    @RequestMapping("/editEmp")
    @ResponseBody
    public Result editEmp(Employee employee, Integer empId, HttpSession session) {
        Employee sessionEmp = (Employee) session.getAttribute("sessionEmp");
        Result result = new Result();
        //判断员工id
        if (empId == 0) {
            //走新增
            employee.setCreateBy(sessionEmp.geteId());
            result = employeeRemote.addEmp(employee,sessionEmp.geteId());
            if (result.isSuccess()) {
                result.setMsg("添加成功");
            } else {
                result.setMsg("添加失败");
            }
        } else {
            //走修改
            employee.seteId(empId);
            employee.setModifyBy(sessionEmp.geteId());
            result = employeeRemote.updEmp(employee,sessionEmp.geteId());
            if (result.isSuccess()) {
                result.setMsg("修改成功");
            } else {
                result.setMsg("修改失败");
            }
        }
        return result;
    }

    //删除
    @RequestMapping("/delEmpById")
    @ResponseBody
    public Result delEmpById(Integer eId, HttpSession session) {
        //获取当前操作人
        Employee sessionEmp = (Employee) session.getAttribute("sessionEmp");
        CurrentUser currentUser = new CurrentUser();
        currentUser.setcEmp(sessionEmp);
        Result result = employeeRemote.delEmp(eId,currentUser);
        if (result.isSuccess()) {
            result.setMsg("删除成功");
        } else {
            result.setMsg("删除失败");
        }
        return result;
    }



}
